package org.testis.wirelesssecuritymodule.core;

import org.testis.data.AtofModuleConstants;
import org.testis.utils.OsInfo;
import org.testis.utils.StringUtils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

/**
 *
 * @author barisergun
 */
public class WirelessSecurityProtocol {

    private WirelessSecuritysProtocolImpl m_OsImpl;
    protected String m_profileString;
    protected String m_wlanProfileString;
    protected String m_iFaceName;
    protected String m_ssid;

    public WirelessSecurityProtocol() {
        if (OsInfo.isWindows()) {
            m_OsImpl = new WirelessSecuritysProtocolWindowsImpl();
        } else  if (OsInfo.isLinux()) {
            m_OsImpl = new WirelessSecuritysProtocolLinuxImpl();
        } else {
            throw new UnsupportedOS("Running on unsupported OS...");
        }
    }

    public boolean createProfile() {
        try {

            Writer output = null;
            m_profileString =
                    m_OsImpl.createProfileString(m_profileString);

            String profileFolder = AtofModuleConstants.ATOF_USER_TEMP_DIRECTORY + "/" +
                    StringUtils.removeSpaces(m_iFaceName);

            File folder = new File(profileFolder);
            if (!folder.exists()) {
                folder.mkdir();
            }


            String profileFile = profileFolder + "/" + m_ssid + ".conf";
            output =
                    new BufferedWriter(new FileWriter(profileFile));
            output.write(m_profileString);
            output.close();
            if (m_OsImpl instanceof WirelessSecuritysProtocolWindowsImpl) {
                String profileWlanFile = profileFolder + "/" + m_ssid + ".xml";
                output =
                        new BufferedWriter(new FileWriter(profileWlanFile));
                output.write(m_wlanProfileString);
                output.close();
            }
            return true;
        } catch (IOException ex) {

            System.out.println(ex.getMessage());
            return false;
        }
    }

    public String getIFaceName() {
        return m_iFaceName;
    }

    public String getProfileString() {
        return m_profileString;
    }

    public String getSsid() {
        return m_ssid;
    }

    /**
     * @return the m_wlanProfileString
     */
    public String getWlanProfileString() {
        return m_wlanProfileString;
    }

    /**
     * @param m_wlanProfileString the m_wlanProfileString to set
     */
    public void setWlanProfileString(String wlanProfileString) {
        this.m_wlanProfileString = wlanProfileString;
    }
}
